package stack;

import java.util.Stack;
//                                                 最小栈
class MinStack {

    private Stack<Integer> stack1 = new Stack<>();
    private Stack<Integer> stack2 = new Stack<>();

    public MinStack() {

    }

    public void push(int val) {
        stack1.push(val);
        if (stack2.isEmpty()){
            stack2.push(val);
            return;
        }
        int min = stack2.peek();
        if (val < min){
            stack2.push(val);
        } else {
            stack2.push(min);
        }

    }

    public void pop() {
        if (stack1.isEmpty()){
            return;
        }
        stack1.pop();
        stack2.pop();
    }

    public int top() {
        if (stack1.isEmpty()){
            return 0;
        }
        return stack1.peek();
    }

    public int getMin() {
        return stack2.peek();
    }
}
